//
//  CosomViewController.m
//  tp1-gti350
//
//  Created by Coulon-Hamelin, Alexandre on 13-09-12.
//  Copyright (c) 2013 Coulon-Hamelin, Alexandre. All rights reserved.
//

#import "CosomViewController.h"


@interface CosomViewController ()

@end

@implementation CosomViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.game = [[HockeyCosom alloc] init];
    self.nbCurrentPeriod.text = @"1";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addPlayerTeam1:(id)sender {
    if([self.game addPlayer:1 AndPlayerLastName:self.playerLastName.text AndPlayerFirstName:self.playerFirstName.text AndPlayerNumber:self.playerNumber.text.intValue])
    {
        [self reloadList:self.listTeam1];
        self.playerFirstName.text = @"";
        self.playerLastName.text = @"";
        self.playerNumber.text = @"";
    }
}

- (IBAction)addPlayerTeam2:(id)sender {
    if([self.game addPlayer:2 AndPlayerLastName:self.playerLastName.text AndPlayerFirstName:self.playerFirstName.text AndPlayerNumber:self.playerNumber.text.intValue])
    {
        [self reloadList:self.listTeam2];
        self.playerFirstName.text = @"";
        self.playerLastName.text = @"";
        self.playerNumber.text = @"";
    }
}

- (IBAction)generateThreeStars:(id)sender {
    [self.game generateThreeStars];
    self.bestPlayersList.text = [self.game getBestPlayersStringList];
}

- (IBAction)addGoal:(id)sender {
    
    [self showAlertAddGoal];
    
}

- (IBAction)btnNameTeam1:(id)sender
{
    [self changeTeamName : 1];
}

- (IBAction)btnNameTeam2:(id)sender
{
    [self changeTeamName : 2];
}

- (IBAction)newGame:(id)sender
{
    self.game = [[HockeyCosom alloc] init];
    
    self.nbCurrentPeriod.text = @"1";
    self.periodTracker.value = 1;
    
    self.lblNameTeam1.text = self.game.team1.name;
    self.lblNameTeam2.text = self.game.team2.name;
    
    self.playerNumber.text = @"";
    self.playerFirstName.text = @"";
    self.playerLastName.text = @"";
    
    self.scoreTeam1.text = @"0";
    self.scoreTeam2.text = @"0";
    
    self.txtNameTeam1.text = @"";
    self.txtNameTeam2.text = @"";
    
    self.scoreBoard.text =  @"";
    self.listTeam1.text = @"";
    self.listTeam2.text = @"";
    self.bestPlayersList.text = @"";
}

- (void)changeTeamName: (int) teamNb
{
    if(teamNb == 1)
    {
        if(!([self.txtNameTeam1.text isEqual: self.game.team2.name]))
        {
            if(!([self.txtNameTeam1.text isEqual: @""]))
            {
                self.game.team1.name = self.txtNameTeam1.text;
                self.lblNameTeam1.text = self.game.team1.name;
            }
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Nom équivalent" message:@"Les deux équipes ne peuvent pas avoir le même nom!" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
            [alert show];
        }
    }
    else if(teamNb == 2)
    {
        if(!([self.txtNameTeam2.text isEqual: self.game.team1.name]))
        {
            if(!([self.txtNameTeam2.text isEqual: @""]))
            {
                self.game.team2.name = self.txtNameTeam2.text;
                self.lblNameTeam2.text = self.game.team2.name;
            }
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Nom équivalent" message:@"Les deux équipes ne peuvent pas avoir le même nom!" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
            [alert show];
        }
    }
}


- (IBAction)addPeriod:(id)sender {
    self.game.currentPeriod = (int)self.periodTracker.value;
    self.nbCurrentPeriod.text = [NSString stringWithFormat:@"%i", self.game.currentPeriod];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if(textField == self.txtNameTeam1)
    {
        [self.txtNameTeam1 resignFirstResponder];
        [self changeTeamName : 1];
    }
    else if(textField == self.txtNameTeam2)
    {
        [self.txtNameTeam2 resignFirstResponder];
        [self changeTeamName : 2];
    }
    else if(textField == self.playerFirstName)
    {
        [self.playerFirstName resignFirstResponder];
    }
    else if(textField == self.playerLastName)
    {
        [self.playerLastName resignFirstResponder];
    }
    else if(textField == self.playerNumber)
    {
        [self.playerNumber resignFirstResponder];
    }
    
    return YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 1)
    {
        if (alertView == self.alertviewGoal) {
            [self newGoal];
        }
    }
}

- (void)showAlertAddGoal
{
    self.alertviewGoal = [[UIAlertView alloc] initWithTitle:@"Nouveau But" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    
    // Insert the team number text field
    self.teamNumber = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    self.teamNumber.placeholder = @"Numéro équipe";
    
    // insert the scorer number text field
    self.scorer = [[UITextField alloc] initWithFrame:CGRectMake(12, 62, 260, 25)];
    self.scorer.placeholder = @"Numéro buteur";
    
    // pass 1
    self.pass1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 78, 260, 25)];
    self.pass1.placeholder = @"Numéro passeur 1";
    
    // pass 2
    self.pass2 = [[UITextField alloc] initWithFrame:CGRectMake(12, 95, 260, 25)];
    self.pass2.placeholder = @"Numéro passeur 2";
    
    [self.alertviewGoal addSubview:self.teamNumber];
    [self.alertviewGoal addSubview:self.scorer];
    [self.alertviewGoal addSubview:self.pass1];
    [self.alertviewGoal addSubview:self.pass2];
    
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 80);
    [self.alertviewGoal setTransform:transform];
    
    // Show alert on screen.
    [self.alertviewGoal show];
}

- (void)newGoal
{
    int teamNumber = [self.teamNumber.text intValue ];
    int scorerNumber = [self.scorer.text intValue];
    int pass1Number = [self.pass1.text intValue];
    int pass2Number = [self.pass2.text intValue];
    
    if ([self.game isValidTeamNumber : teamNumber]) {
        
        Team *teamTmp =  [self.game getTeamByNumber:teamNumber];
        
        if ([teamTmp isValidPlayerNumber:scorerNumber]) {
            
            Boolean validPassers = true;
            Player * scorerTmp = [teamTmp getPlayerByNumber:scorerNumber];
            Player * pass1Tmp = [Player alloc];
            Player * pass2Tmp = [Player alloc];
            
            if(pass1Number != 0)
            {
                if([teamTmp isValidPlayerNumber:pass1Number])
                {
                    pass1Tmp = [teamTmp getPlayerByNumber:pass1Number];
                }
                else
                {
                    validPassers = false;
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Numéro de joueur incorrect" message:@"Le numéro du joueur de la passe 1 est incorrect" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
                    [alert show];
                }
            }
            else
            {
                pass1Tmp.lastName = @"sans aide";
            }

            
            if(pass2Number != 0)
            {
                if([teamTmp isValidPlayerNumber:pass1Number])
                {
                    pass2Tmp = [teamTmp getPlayerByNumber:pass2Number];
                }
                else
                {
                    validPassers = false;
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Numéro de joueur incorrect" message:@"Le numéro du joueur de la passe 2 est incorrect" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
                    [alert show];
                }
            }
            else
            {
                pass2Tmp.lastName = @"sans aide";
            }

            if(validPassers)
            {
                [self.game addGoal:teamNumber AndScorer:scorerTmp AndPass1:pass1Tmp AndPass2:pass2Tmp];
            }
            
            if(teamNumber == 1)
                self.scoreTeam1.text = [NSString stringWithFormat:@"%i", [self.game getTeamScore: 1]];
            else
                self.scoreTeam2.text = [NSString stringWithFormat:@"%i", [self.game getTeamScore: 2]];
            
            [self reloadList:self.scoreBoard];

        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Numéro de joueur incorrect" message:@"Le numéro du buteur est incorrect" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
            [alert show];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Numéro Equipe incorrect" message:@"Vous avez saisi un numéro d'équipe incorrect" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
        [alert show];
    }
}

- (void)reloadList:(UITextView *) list

{
    if (list == self.scoreBoard) {
        self.scoreBoard.text = self.game.formatedGoalString;
    }
    if (list == self.listTeam1) {
        self.listTeam1.text = [self.game.team1 getOneStringFormatedPlayerList];
    }
    if (list == self.listTeam2) {
        self.listTeam2.text = [self.game.team2 getOneStringFormatedPlayerList];
    }
}
@end
